﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace Common.Utils
{
    public class StringFormatHelp
    {
        public static string ToString(object obj, Type type)
        {
            if (obj==null)
            {
                return "null";
            }

            try
            {
                //可序列化
                if (type.IsSerializable)
                {
                   return JsonConvert.SerializeObject(obj);
                }

                //if (type.IsArray)
                //{
                    
                //}

                return obj.ToString();
            }
            catch (Exception e)
            {
                return obj.ToString();
            }
        }

        public static string ToString(object obj)
        {
            if (obj == null)
            {
                return "null";
            }

            Type type = obj.GetType();

            try
            {
                //可序列化
                if (type.IsSerializable)
                {
                    return JsonConvert.SerializeObject(obj);
                }

                //if (type.IsArray)
                //{

                //}

                return obj.ToString();
            }
            catch (Exception e)
            {
                return obj.ToString();
            }
        }
    }
}
